package com.wenda.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wenda.bean.ThesisBean;
import com.wenda.lucene.ThesisIndex;
import com.wenda.service.SearchService;
import com.wenda.util.DateUtil;

@Service("searchService")
public class SearchServiceImpl implements SearchService {
	@Resource
	ThesisIndex thesisIndex;

	@Override
	public List<ThesisBean> query(String title, Integer collegeId, boolean flag) throws Exception {
		List<ThesisBean> thesisBeans = thesisIndex.searchThesis(title);
		List<ThesisBean> nowResult = new ArrayList<>();
		List<ThesisBean> oldResult = new ArrayList<>();
		// flag 表示用户是否是查询现届
		String nowDate = DateUtil.formatDate(new Date(), "yyyy");
		for (ThesisBean thesisBean : thesisBeans) {
			String thesisDate = DateUtil.formatDate(thesisBean.getCreateDate(), "yyyy");
			//System.out.println(thesisDate);
			if (nowDate.equals(thesisDate) && collegeId == thesisBean.getCollegeId()) {
				nowResult.add(thesisBean);
			}else if(collegeId == thesisBean.getCollegeId()) {
				oldResult.add(thesisBean);
			}
			/*if (nowDate.equals(thesisDate)) {
				nowResult.add(thesisBean);
			}else{
				oldResult.add(thesisBean);
			}*/
		}
		if(flag) {
			return nowResult;
		} else{
			return oldResult;
		}
	}
}